package com.smt.common.aspect;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.smt.common.utils.Constant;
import com.smt.modules.bus.entity.ProjectTaskEntity;
import com.smt.modules.bus.entity.SubtaskEntity;
import com.smt.modules.bus.service.ProjectTaskService;
import com.smt.modules.bus.service.SubtaskService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.joda.time.DateTime;
import org.joda.time.Days;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * 设置任务/子任务过期时间天数
 *
 * @author zhangyi
 * @date 2018/10/27 15:57
 */
@Component
@Aspect
@Slf4j
public class ConceptualAspect {
    @Autowired
    private ProjectTaskService projectTaskService;
    @Autowired
    private SubtaskService subtaskService;

    @Pointcut("@annotation(com.smt.modules.app.annotation.Conceptual)")
    public void c(){};

    @Before("c()")
    public void fun(){
        List<ProjectTaskEntity> projectTaskEntityList =
                projectTaskService.selectList(new EntityWrapper<ProjectTaskEntity>()
                .eq("status",Constant.SQL_CON));
        List<SubtaskEntity> subtaskEntities =
                subtaskService.selectList(new EntityWrapper<SubtaskEntity>()
                .eq("status",Constant.SQL_CON));
        DateTime now = DateTime.now();
        projectTaskEntityList.stream()
                .forEach(s -> {
                    Date endTime = s.getEndTime();
                    if(endTime != null) {
                        DateTime dateTime = new DateTime(endTime);
                        int days = Days.daysBetween(now, dateTime).getDays();
                        ProjectTaskEntity projectTaskEntity = new ProjectTaskEntity();
                        projectTaskEntity.setId(s.getId());
                        if (days < 0) {
                            projectTaskEntity.setStatus(2);
                            log.info("任务过期:{}", projectTaskEntity.getId());
                            projectTaskService.updateById(projectTaskEntity);
                        }
                    }
                });
        subtaskEntities.stream()
                .forEach(sub -> {
                    Date endTime = sub.getEndTime();
                    if(endTime != null) {
                        DateTime dateTime = new DateTime(endTime);
                        int days = Days.daysBetween(now, dateTime).getDays();
                        SubtaskEntity subtaskEntity = new SubtaskEntity();
                        subtaskEntity.setId(sub.getId());
                        if (days < 0) {
                            subtaskEntity.setStatus(2);
                            log.info("子任务过期:{}", subtaskEntity.getId());
                            subtaskService.updateById(subtaskEntity);
                        }
                    }
                });
    }
}
